<?php


namespace addons\Minerva\api\modules\v1\controllers;

use Yii;
use api\controllers\OnAuthController;
use common\models\common\ActionLog;
use yii\data\ActiveDataProvider;
use common\helpers\DebrisHelper;

/**
 * LogController.php
 *
 * @Class LogController
 * @Aackage addons\Minerva\api\modules\v1\controllers
 * @Author 小武哥<1240035958@qq.com>
 * @Date 2021/10/14 17:55
 * @Version 1.0
 */
class LogController extends OnAuthController
{
    public $modelClass = '';

    public function actionAclog(){
        $get = Yii::$app->request->get();
        $list = new ActiveDataProvider([
            'query' => ActionLog::find()
                ->with(['backendMember'])
                ->orderBy('created_at desc')
                ->select('id,app_id,user_id,behavior,url,ip,remark,created_at')
                ->asArray(),
            'pagination' => [
                'pageSize' => $this->pageSize,
                'validatePage' => false,// 超出分页不返回data
            ],
        ]);
        $model = $list->getModels();
        foreach ($model as $key => $val){
            $ip = [];
            $ip[] = DebrisHelper::analysisIp($val['ip']);
            $ip[] = DebrisHelper::long2ip($val['ip']);
            $model[$key]['ip'] = $ip;
            if(!empty($val['backendMember'])){
                $user = [];
                $user[] = "ID：" . $val['backendMember']['id'];
                $user[] = "账号：" . $val['backendMember']['username'];
                $user[] = "姓名：" . $val['backendMember']['realname'];
                $model[$key]['user'] = $user;
            }else{
                $model[$key]['user'] = '游客';
            }
            unset($model[$key]['backendMember']);
        }
        $list->setModels($model);
        return $list;
    }
}